<?php
	define('CONFIG_FILE',true);
	include '../config.php';
	
	define('DB_FILE',true);
	require_once '../Tool/DB/SqlDBManager.class.php';

	session_start();
	if (empty($_SESSION["userType"])) {
		$url="../Index.php";
		include_once '../Tool/Common/Redirect.php';
		exit();
	}

	if($_SESSION["userType"]=="Therapy Assistant"){
        $url="../Index.php";
		include_once '../Tool/Common/Redirect.php';
		exit();
    }
	
	$ActivityId = intval($_REQUEST['ActivityId']);
	$ActivityName= $_REQUEST['ActivityName'];
	$Category = $_REQUEST['Category'];
	$CentreName=$_REQUEST['CentreName'];

	$sqlDBManager = new SqlDBManager();

	$sql = "SELECT * from Activity where ActivityId=?";
    $parameters = array($ActivityId);
    $res=$sqlDBManager->queryRow($sql,$parameters);

    if(!empty($res)){
    	$ActivityIdExisted=$res['ActivityId'];
        $ActivityNameExisted=$res['ActivityName'];
        $CategoryExisted=$res['Category'];
        $CentreNameExisted=$res['CentreName'];

        //If no changes, return ture else execute update statement
        if($ActivityId==$ActivityIdExisted&&$ActivityName==$ActivityNameExisted&&$Category==$CategoryExisted&&$CentreName==$CentreNameExisted){
            echo json_encode(array('success'=>true));   
        }else{
			$sql = "UPDATE activity SET ActivityName=?,Category=?,CentreName=? WHERE ActivityId=?";
			$parameters = array($ActivityName,$Category,$CentreName,$ActivityId);
			$res=$sqlDBManager->execute_dml($sql,$parameters);

			//close connection
			$sqlDBManager->close_connect();
			
			if ($res>0){
				echo json_encode(array('success'=>true));
			}else {
				echo json_encode(array('msg'=>'Update activity failed due to database error.'));
			}
		}
	}
?>